/*
 * title: 公共过滤器
 * date: 2019-05-15
 * author: wjh
 */

import Vue from 'vue'
import dayjs from 'dayjs'
import imgDict from './img'

// 时间戳转日期时间
Vue.filter('fmtDateTime', (val, fmt = 'YYYY-MM-DD HH:mm:ss') => {
  val = val ? dayjs(val).format(fmt) : '—'
  return val
})

// 时间戳转日期
Vue.filter('fmtDate', (val, fmt = 'YYYY-MM-DD') => {
  val = val ? dayjs(val).format(fmt) : '—'
  return val
})

// 手机号中间四位隐藏
Vue.filter('hidePhone', val => {
  return val ? val.replace(/(\d{3})(\d{4})(\d{4})/, '$1****$3') : '—'
})

// 空值显示为--
Vue.filter('across', val => {
  return (val || val === 0) ? val : '—'
})

// tag对dict
Vue.filter('tagFmt', (val, fmt, type) => {
  if (!type) {
    if (val) return fmt[val]
  } else {
    try {
      fmt.forEach(item => {
        if (item.key === type) {
          if (item.children && item.children.length) { // 有二级往下走，返回children.name
            item.children.forEach(children => {
              if (children.key === val) {
                throw new Error(children.name)
              }
            })
          } else { // 一级，返回item.name
            throw new Error(item.name)
          }
        }
      })
    } catch (err) {
      return err.message
    }
  }
})

// tag对type
Vue.filter('tagType', val => {
  let type = {
    1: 'danger',
    2: 'success',
    3: 'warning',
    4: 'primary',
    5: 'purple',
    6: 'pink'
  }
  if (val) return type[val]
})

// 默认图
Vue.filter('picFmt', val => {
  return val || imgDict.logo
})

// 千分位
Vue.filter('thousand', val => {
  return (val || val === 0) ? val.toString().replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g, '$&,') : 0
})

// 金额保留两位小数
Vue.filter('moneyFmt', (val, fmt = 2) => {
  return parseFloat(val || 0).toFixed(fmt)
})

// 阿拉伯数字转人民币的中文
Vue.filter('toRMB', money => {
  var cnNums = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖']
  var cnIntRadice = ['', '拾', '佰', '仟']
  var cnIntUnits = ['', '万', '亿', '兆']
  var cnDecUnits = ['角', '分', '毫', '厘']
  var cnInteger = '整'
  var cnIntLast = '元'
  var IntegerNum
  var DecimalNum
  var ChineseStr = ''
  var parts
  if (money === '') {
    return ''
  }
  money = parseFloat(money)
  if (money === 0) {
    ChineseStr = cnNums[0] + cnIntLast + cnInteger
    return ChineseStr
  }
  money = money.toString()
  if (money.indexOf('.') === -1) {
    IntegerNum = money
    DecimalNum = ''
  } else {
    parts = money.split('.')
    IntegerNum = parts[0]
    DecimalNum = parts[1].substr(0, 4)
  }
  if (parseInt(IntegerNum, 10) > 0) {
    var zeroCount = 0
    var IntLen = IntegerNum.length
    for (var i = 0; i < IntLen; i++) {
      var n = IntegerNum.substr(i, 1)
      var p = IntLen - i - 1
      var q = p / 4
      var m = p % 4
      if (n === '0') {
        zeroCount++
      } else {
        if (zeroCount > 0) {
          ChineseStr += cnNums[0]
        }
        zeroCount = 0
        ChineseStr += cnNums[parseInt(n)] + cnIntRadice[m]
      }
      if (m === 0 && zeroCount < 4) {
        ChineseStr += cnIntUnits[q]
      }
    }
    ChineseStr += cnIntLast
  }
  if (DecimalNum !== '') {
    var decLen = DecimalNum.length
    for (var j = 0; j < decLen; j++) {
      var n1 = DecimalNum.substr(j, 1)
      if (n1 !== '0') {
        ChineseStr += cnNums[Number(n1)] + cnDecUnits[j]
      }
    }
  }
  if (ChineseStr === '') {
    ChineseStr += cnNums[0] + cnIntLast + cnInteger
  } else if (DecimalNum === '') {
    ChineseStr += cnInteger
  }
  return ChineseStr
})
